package cn.iocoder.yudao.module.system.service.partnercouponshare;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.partnercouponshare.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.partnercouponshare.PartnerCouponShareDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.partnercouponshare.PartnerCouponShareMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 合伙人优惠券分享记录 Service 实现类
 *
 * @author 万民匠心
 */
@Service
@Validated
public class PartnerCouponShareServiceImpl implements PartnerCouponShareService {

    @Resource
    private PartnerCouponShareMapper partnerCouponShareMapper;

    @Override
    public Long createPartnerCouponShare(PartnerCouponShareSaveReqVO createReqVO) {
        // 插入
        PartnerCouponShareDO partnerCouponShare = BeanUtils.toBean(createReqVO, PartnerCouponShareDO.class);
        partnerCouponShareMapper.insert(partnerCouponShare);
        // 返回
        return partnerCouponShare.getId();
    }

    @Override
    public void updatePartnerCouponShare(PartnerCouponShareSaveReqVO updateReqVO) {
        // 校验存在
        validatePartnerCouponShareExists(updateReqVO.getId());
        // 更新
        PartnerCouponShareDO updateObj = BeanUtils.toBean(updateReqVO, PartnerCouponShareDO.class);
        partnerCouponShareMapper.updateById(updateObj);
    }

    @Override
    public void deletePartnerCouponShare(Long id) {
        // 校验存在
        validatePartnerCouponShareExists(id);
        // 删除
        partnerCouponShareMapper.deleteById(id);
    }

    private void validatePartnerCouponShareExists(Long id) {
        if (partnerCouponShareMapper.selectById(id) == null) {
            throw exception(PARTNER_COUPON_SHARE_NOT_EXISTS);
        }
    }

    @Override
    public PartnerCouponShareDO getPartnerCouponShare(Long id) {
        return partnerCouponShareMapper.selectById(id);
    }

    @Override
    public PageResult<PartnerCouponShareDO> getPartnerCouponSharePage(PartnerCouponSharePageReqVO pageReqVO) {
        return partnerCouponShareMapper.selectPage(pageReqVO);
    }

}